
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.BaseStoreListBean;
import com.epfresh.supplier.entity.CategoryNames;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class AgentMakeBaseGoodsViewBinder
    extends ItemViewBinder<BaseStoreListBean.ProductsBean, AgentMakeBaseGoodsViewBinder.ViewHolder> implements View.OnClickListener{

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    int imageWidth = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(3))/2;
    int actHeight = LocalDisplay.SCREEN_WIDTH_PIXELS / 3;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_make_agent_goods, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull BaseStoreListBean.ProductsBean post) {
//        holder.setData(post);
        holder.itemView.setTag(R.id.item_key_position,holder.getAdapterPosition());
        holder.itemView.setTag(R.id.item_key_status,post);
        holder.itemView.setOnClickListener(this);
        initPicAndTag(holder,post);
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
            if (onRecyclerViewItemClickListener != null && o instanceof BaseStoreListBean.ProductsBean){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView cover;
        private TextView title;
        private View line;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            cover = (ImageView) itemView.findViewById(R.id.iv_make_agent_goods);
            title = (TextView) itemView.findViewById(R.id.tv_make_agent_name);

        }


        void setData(@NonNull final BaseStoreListBean.ProductsBean post) {
//            cover.setImageResource(post.coverResId);
            title.setText(post.getTitle());
        }
    }
    void initPicAndTag(@NonNull ViewHolder holder, @NonNull BaseStoreListBean.ProductsBean bean) {
        ViewGroup.LayoutParams params = holder.cover.getLayoutParams();
        params.width = imageWidth;
        params.height = actHeight;

        holder.cover.setLayoutParams(params);
            ImageLoader.getInstance().displayImage(bean.getAttachmentUrl(), holder.cover);
    }
}
